﻿using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfComm.ViewModels
{
    public delegate void delete(int id);

    class MainWindowViewModel
    {
        public ObservableCollection<TabItem> Tabs { get; set; }

        public DelegateCommand CmdAddTabView { get; set; }  //添加 TabView

        public MainWindowViewModel()
        {
            Tabs = new ObservableCollection<TabItem>();
            Tabs.Add(new TabItem { Id = Tabs.Count, Header = "One", Content = "One's content",Del = deletebyid });
            Tabs.Add(new TabItem { Id = Tabs.Count, Header = "Two", Content = "Two's content", Del = deletebyid });

            this.CmdAddTabView = new DelegateCommand(new Action(this.cmdAddTabView));//添加 TabView
        }
        private void cmdAddTabView()
        {           
            Tabs.Add(new TabItem { Id = Tabs.Count, Header = "AAA", Content = "Aaa's content", Del = deletebyid });
        }

        private void deletebyid(int id) {
            int sid=0;
            bool finded = false;
            for (int i = 0; i < Tabs.Count; i++)
            {
                if (Tabs[i].Id == id)
                {
                    sid = i;
                    finded = true;
                    break; 
                }
            }
            if (finded == true)
            {
                Tabs.RemoveAt(sid);
            }

        }
    }
    public sealed class TabItem
    {
        public int Id;
        public string Header { get; set; }
        public string Content { get; set; }
        public DelegateCommand CmdDeletSelf { get; set; }  //添加 TabView        
        private SerialPortSetViewModel _serialPortSetViewModel;

        public SerialPortSetViewModel serialPortSetViewModel
        {
            get { return _serialPortSetViewModel; }
            set { _serialPortSetViewModel = value; }
        }

        public delete Del
        {
            get
            {
                return del;
            }

            set
            {
                del = value;
            }
        }

        delete del;
        public TabItem()
        {
            serialPortSetViewModel = new SerialPortSetViewModel();
            this.CmdDeletSelf = new DelegateCommand(new Action(this.cmdDeletSelf));//添加 TabView
        }

        private void cmdDeletSelf()
        {
            Del(Id);
        }
    }
}
